extends "res://BaseEnemy.gd"

# 沙漠领主Boss特有属性
@export var phase = 1  # 当前阶段
@export var phase_health_threshold = 0.5  # 进入第二阶段的生命值阈值
@export var sandstorm_cooldown = 15.0  # 沙尘暴冷却时间
@export var sandstorm_duration = 5.0  # 沙尘暴持续时间
@export var sandstorm_damage = 3  # 沙尘暴每秒伤害
@export var minion_summon_cooldown = 10.0  # 随从召唤冷却时间
@export var sand_blast_cooldown = 5.0  # 沙爆冷却时间
@export var sand_blast_damage = 20  # 沙爆伤害

var sandstorm_timer = 0.0
var minion_timer = 0.0
var sand_blast_timer = 0.0
var can_sandstorm = true
var can_summon = true
var can_sand_blast = true
var is_sandstorm_active = false
var sandstorm_active_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置沙漠领主Boss特有属性
	max_health = 500
	current_health = max_health
	movement_speed = 70.0
	damage = 15
	experience_value = 100
	is_boss = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
		health_bar.visible = true  # Boss始终显示生命值条
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(1.5, 1.2, 0.7)  # Boss颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
	
	# 处理沙尘暴冷却
	if not can_sandstorm:
		sandstorm_timer += delta
		if sandstorm_timer >= sandstorm_cooldown:
			can_sandstorm = true
			sandstorm_timer = 0.0
	
	# 处理随从召唤冷却
	if not can_summon:
		minion_timer += delta
		if minion_timer >= minion_summon_cooldown:
			can_summon = true
			minion_timer = 0.0
	
	# 处理沙爆冷却
	if not can_sand_blast:
		sand_blast_timer += delta
		if sand_blast_timer >= sand_blast_cooldown:
			can_sand_blast = true
			sand_blast_timer = 0.0
	
	# 处理沙尘暴效果
	if is_sandstorm_active:
		sandstorm_active_timer += delta
		
		# 每秒对玩家造成伤害
		if target_player and is_instance_valid(target_player) and sandstorm_active_timer >= 1.0:
			sandstorm_active_timer = 0.0
			target_player.take_damage(sandstorm_damage)
			
			# 减速效果
			if target_player.has_method("apply_slow_effect"):
				target_player.apply_slow_effect(0.7, 1.5)
		
		# 沙尘暴结束
		if sandstorm_timer >= sandstorm_duration:
			is_sandstorm_active = false
	
	# 根据阶段和冷却状态使用技能
	if phase == 2:
		# 第二阶段更积极地使用技能
		if can_sandstorm and randf() < 0.01:  # 每帧1%几率
			activate_sandstorm()
		
		if can_summon and randf() < 0.005:  # 每帧0.5%几率
			summon_minions()
		
		if can_sand_blast and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 150 and randf() < 0.02:  # 近距离时每帧2%几率
				sand_blast_attack()
	else:
		# 第一阶段较少使用技能
		if can_sandstorm and randf() < 0.005:  # 每帧0.5%几率
			activate_sandstorm()
		
		if can_summon and randf() < 0.003:  # 每帧0.3%几率
			summon_minions()
		
		if can_sand_blast and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 150 and randf() < 0.01:  # 近距离时每帧1%几率
				sand_blast_attack()

func enter_phase_two():
	phase = 2
	
	if animation_player and animation_player.has_animation("transform"):
		animation_player.play("transform")
	
	# 增强属性
	movement_speed *= 1.3
	damage *= 1.5
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(1.8, 1.4, 0.6)  # 更亮的颜色
		sprite.scale *= 1.2  # 更大的尺寸
	
	# 立即使用沙尘暴
	activate_sandstorm()
	
	print("沙漠领主进入第二阶段!")

func activate_sandstorm():
	if animation_player and animation_player.has_animation("sandstorm"):
		animation_player.play("sandstorm")
	
	# 设置沙尘暴状态
	is_sandstorm_active = true
	sandstorm_active_timer = 0.0
	can_sandstorm = false
	sandstorm_timer = 0.0
	
	# 创建沙尘暴效果
	var sandstorm = preload("res://SandstormEffect.tscn").instantiate()
	get_tree().get_root().add_child(sandstorm)
	sandstorm.global_position = global_position
	sandstorm.duration = sandstorm_duration
	
	print("沙漠领主释放沙尘暴!")

func summon_minions():
	if animation_player and animation_player.has_animation("summon"):
		animation_player.play("summon")
	
	# 召唤随从
	var count = 2 if phase == 1 else 3
	for i in range(count):
		var minion_type = randi() % 3
		var minion_scene = ""
		
		match minion_type:
			0: minion_scene = "res://DesertSlime.tscn"
			1: minion_scene = "res://DesertScorpion.tscn"
			2: minion_scene = "res://DesertSkeleton.tscn"
		
		var minion = load(minion_scene).instantiate()
		get_tree().get_root().add_child(minion)
		
		# 设置随从位置（在Boss周围）
		var angle = TAU * i / count
		var offset = Vector2(cos(angle), sin(angle)) * 50
		minion.global_position = global_position + offset
	
	# 重置召唤冷却
	can_summon = false
	minion_timer = 0.0
	
	print("沙漠领主召唤了", count, "个随从!")

func sand_blast_attack():
	if animation_player and animation_player.has_animation("sand_blast"):
		animation_player.play("sand_blast")
	
	# 创建沙爆效果
	var sand_blast = preload("res://SandBlastEffect.tscn").instantiate()
	get_tree().get_root().add_child(sand_blast)
	sand_blast.global_position = global_position
	sand_blast.damage = sand_blast_damage
	sand_blast.radius = 100.0
	sand_blast.source = self
	
	# 重置沙爆冷却
	can_sand_blast = false
	sand_blast_timer = 0.0
	
	print("沙漠领主释放沙爆!")

# 重写攻击方法
func attack_player():
	if phase == 2 and randf() < 0.3:  # 第二阶段30%几率使用特殊攻击
		sand_blast_attack()
	else:
		# 普通攻击
		super.attack_player()

# 重写受伤方法，添加阶段转换逻辑
func take_damage(amount):
	super.take_damage(amount)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
